import Dep from './Dep'
import { observe } from './observe'
export const defineReactive = function (data, key, val) {
  const dep = new Dep()
  if (arguments.length === 2) {
    val = data[key]
  }
  let childOb = observe(val)
  Object.defineProperty(data, key, {
    enumerable: true, //是否可以枚举
    configurable: true,
    get() {
      console.log('get' + ' - ' + key)
      if (Dep.target) {
        dep.depend()
        if (childOb) {
          childOb.dep.depend()
        }
      }
      return val
    },
    set(newVal) {
      console.log('set' + ' - ' + key)
      if (val === newVal) return
      val = newVal
      childOb = observe(newVal)
      // 发布订阅模式
      dep.notify()
    }
  })
}
